<template>
  <div>
    <el-row type="flex" justify="end">
      <!-- 年份和月份的日历下拉筛选框 -->
      <el-select
        v-model="currentYarn"
        size="small"
        style="width: 120px; margin-right: 10px"
        @change="changeDateFn"
      >
        <el-option
          v-for="item in YarnList"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <el-select
        v-model="currentMonth"
        size="small"
        style="width: 120px"
        @change="changeDateFn"
      >
        <el-option v-for="item in 12" :key="item" :label="item" :value="item" />
      </el-select>
    </el-row>
    <el-calendar :v-model="propTime" :value="currentData">
      <template v-slot:dateCell="{ date, data }" class="content">
        <div class="date-content">
          <span class="text"> {{ data.day | getDay }}</span>
          <span v-if="isWeek(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  filters: {
    getDay(data) {
      const day = data.split('-')[2]
      return day.startsWith(0) ? day.substr(1) : day
    }
  },
  props: {
    propTime: {
      type: Date,
      default: () => new Date()
    }
  },
  data() {
    return {
      YarnList: [], // 要遍历的年份
      currentYarn: null, // 当前年份
      currentMonth: null, // 当前的月份
      currentData: null, // 当前时间
      currentday: null // 当前的日期
    }
  },
  created() {
    this.currentYarn = this.propTime.getFullYear()
    this.currentMonth = this.propTime.getMonth() + 1
    this.currentday = this.propTime.getUTCDate()
    this.YarnList = Array.from(Array(11), (v, i) => this.currentYarn + i - 5)
    // 因为默认值 是空 所以需要主动调用一下方法
    this.changeDateFn()
  },
  methods: {
    changeDateFn() {
      this.currentData = new Date(`${this.currentYarn}-${this.currentMonth}-${this.currentday}`)
    },
    isWeek(date) {
      return date.getDay() === 6 || date.getDay() === 0
    }
  }
}
</script>

<style scoped>
/deep/ .el-calendar-day {
  height: auto;
}
/deep/ .el-calendar-table__row td,
/deep/ .el-calendar-table tr td:first-child,
/deep/ .el-calendar-table__row td.prev {
  border: none;
}
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text {
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
}
/deep/ .el-calendar-table td.is-selected .text {
  background: #409eff;
  color: #fff;
  border-radius: 50%;
}
/deep/ .el-calendar__header {
  display: none;
}
</style>
